import React from 'react';
import { Table } from 'antd';
import Actions from 'src/components/Actions';
import { findVariableName, getVariableName, getVariableValue } from 'src/utils/variable';
import ResultStyle from './resultStyle';
import { DATA_TYPE_NUMBER, DATA_TYPE_BOOLEAN } from '../../../../../utils/constant';
import './index.less';
import ConditionsIndex from 'src/components/Conditions/ConditionsIndex';

const RulesScore = (props) => {
  const { logs = {}, value, actions, variable, using_variables } = props;
  const { rules, variables } = logs;
  const logIdList = rules && rules.map((item) => item.hit && item.id);
  const isHit = (id) => (rules.find((item) => item.id === id) || {}).hit;
  // 根据变量类型判断分箱类型
  // const getValueByType = (min, max, value, data_type) => {
  //   return DATA_TYPE_NUMBER.includes(data_type) || data_type === null || data_type === undefined
  //     ? `[${min}, ${max})`
  //     : DATA_TYPE_BOOLEAN.includes(data_type)
  //     ? value
  //     : '';
  // };

  const detailColumns = [
    {
      title: '序号',
      dataIndex: 'index',
      render: (_, __, index) => index + 1,
    },
    {
      title: '左变量',
      dataIndex: 'left_var_name',
      render: (_, { left_var_code, left_var_type }) => {
        const tempVarName =
          using_variables && using_variables.length && using_variables.find((item) => item.code === left_var_code)
            ? getVariableName(left_var_code, using_variables)
            : findVariableName(left_var_type, left_var_code, variable);
        return tempVarName;
      },
    },
    {
      title: '变量类型',
      dataIndex: 'left_var_data_type',
      render: (text) =>
        DATA_TYPE_NUMBER.includes(text) || text === null || text === undefined
          ? '数值类型'
          : DATA_TYPE_BOOLEAN.includes(text)
          ? '枚举类型'
          : '',
    },
    {
      title: '分箱数',
      dataIndex: 'cases.length',
    },
    {
      title: '默认分',
      dataIndex: 'default_score',
    },
  ];

  const expandedRowRender = ({ cases, left_var_code, left_var_name, left_var_type }) => {
    const columns = [
      {
        title: '分箱区间',
        key: 'conditions',
        render: (record) => {
          // return getValueByType(min, max, value, left_var_data_type);
          return <ConditionsIndex
            {...props}
            disabled={true}
            value={record.conditions}
            leftDisable={true}
            type='cross'
            leftConditionValue={{
              var_code: left_var_code,
              var_name: left_var_name,
              var_type: left_var_type,
            }}
          />
        },
      },
      {
        title: '分值',
        key: 'score',
        dataIndex: 'score',
      },
    ];
    return (
      <Table
        columns={columns}
        dataSource={cases}
        pagination={false}
        rowKey='id'
        bordered
        style={{ padding: '10px 0' }}
      />
    );
  };

  const resultColumns = [
    {
      title: '序号',
      dataIndex: 'index',
      render: (_, __, index) => index + 1,
    },
    {
      title: '左变量',
      dataIndex: 'left_var_name',
      render: (_, { left_var_code, left_var_type, left_var_name }) => {
        const tempVarName =
          using_variables && using_variables.length && using_variables.find((item) => item.code === left_var_code)
            ? getVariableName(left_var_code, using_variables)
            : findVariableName(left_var_type, left_var_code, variable);
        return (
          <ResultStyle
            top={tempVarName}
            bottom={getVariableValue(
              { var_type: left_var_type, var_name: left_var_name, var_code: left_var_code },
              variables
            )}
          />
        );
      },
    },
    {
      title: '变量类型',
      dataIndex: 'left_var_data_type',
      render: (text) =>
        DATA_TYPE_NUMBER.includes(text) || text === null || text === undefined
          ? '数值类型'
          : DATA_TYPE_BOOLEAN.includes(text)
          ? '枚举类型'
          : '',
    },
    {
      title: '命中区间',
      dataIndex: 'cases',
      render: (_, { id, cases, left_var_code, left_var_type, left_var_name, left_var_data_type }) => {
        const record = cases.find((item) => logIdList.includes(item.id));
        if (record) {
          return (
            <ConditionsIndex
              {...props}
              disabled={true}
              value={record.conditions}
              leftDisable={true}
              type='cross'
              leftConditionValue={{
                var_code: left_var_code,
                var_name: left_var_name,
                var_type: left_var_type,
              }}
              isHit={true}
            />
          );
        }
        return;
      },
    },
    {
      title: '得分',
      dataIndex: '',
      render: (_, { cases, default_score, id }) => {
        const record = cases.find((item) => logIdList.includes(item.id));
        if (record) {
          return <span className='hit'>{record.score}</span>;
        }
        return default_score;
      },
    },
  ];

  const actionColums = [
    {
      title: '得分区间',
      dataIndex: 'min',
      width: 300,
      render: (_, { min, id, max }) => {
        const range = `[${min}, ${max})`;
        return isHasLog ? (
          <ResultStyle tag hightLight isHit={isHit(id)} top={range} bottom={<span>总得分：{getTotalScore()}</span>} />
        ) : (
          range
        );
      },
    },
    {
      title: '动作',
      dataIndex: 'actions',
      render: (text, { id }) => {
        return <Actions {...props} isHit={isHasLog && isHit(id)} value={text} disabled={true} actions={actions} />;
      },
    },
  ];

  // 计算总得分 命中的就拿命中分数 没有命中取默认得分
  const getTotalScore = () => {
    const var_code = `${logs.strategy_code},score`;
    const totalWeight = getVariableValue({ var_code }, variables);
    return totalWeight === 'null' ? '' : totalWeight;
    // const { score_calc_rules } = value;
    // const total_score = score_calc_rules.reduce((prev, { cases, default_score }) => {
    //   const hitObj = cases.find((item) => logIdList.includes(item.id)) || {};
    //   const score = hitObj.score || default_score;
    //   return prev + score;
    // }, 0);
    // return total_score;
  };

  const isHasLog = Object.keys(logs).length > 0;
  return (
    <>
      <div
        style={{
          marginBottom: '20px',
        }}
      >
        <span>
          基准分: {value.base_score || 0}
        </span>
        {isHasLog && <span style={{ marginLeft: 20 }}>
          总得分:
          {<span className='hit'>{getTotalScore()}</span>}
        </span>}
      </div>
      <Table
        columns={isHasLog ? resultColumns : detailColumns}
        dataSource={value.score_calc_rules}
        rowKey='id'
        pagination={false}
        expandedRowRender={!isHasLog && expandedRowRender}
        // expandIconAsCell expandIconColumnIndex 用来隐藏+
        expandIconAsCell={!isHasLog}
        expandIconColumnIndex={isHasLog ? -1 : 0}
        style={{ marginBottom: '20px' }}
      />
      <Table columns={actionColums} dataSource={value.score_range_rules} rowKey='id' pagination={false} />
    </>
  );
};
export default RulesScore;
